package com.share.web.action.sysmgr.privilege;

import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.share.bean.oa.org.EmpAccount;
import com.share.commons.util.MD5;
import com.share.service.oa.org.EmployeeLoginLicenceManager;
import com.share.web.action.base.BaseActionSupport;


/**
 * 说明：验证权限
 * <br/>编写者：杨浩泉
 * <br/>日期：2010-3-1
 */
@Component
@Scope("prototype")
public class ValidateLicenceAction extends BaseActionSupport implements
		ServletRequestAware {

	private static final long serialVersionUID = 1L;

	@Resource
	private EmployeeLoginLicenceManager empMgr ;
	
	private String valResult;

	private String valUrl;
	
	/**
	 * 验证操作权限以及用户是否登录了
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String valdateLicence() throws Exception {
		
		HashMap<String,Object> map = (HashMap) this.getBaseform().getRequest().getSession().getAttribute("roleresMap");
		if(null != map) {
			if(null != map.get("role") || null != map.get("res")) {
				valResult = this.empMgr.validateLicenceManager(map, valUrl) ;
				log.info("验证用户权限...") ;
			} else {
				valResult = "login" ;
				log.info("用户登录超时...") ;
			}
		} else {
			valResult = "login" ;
			log.info("用户登录超时...") ;
		}
		return SUCCESS;
	}
	
	/**
	 * 修改密码前先确认原密码
	 * @return
	 */
	public String modifyChkPwd() {
		EmpAccount emp = (EmpAccount)this.getBaseform().getRequest().getSession().getAttribute("employee") ;
		if(MD5.MD5Encode(valResult).equals(emp.getPassword())) {
			valResult = "true" ;
		} else {
			valResult = "false" ;
		}
		
		return SUCCESS;
	}

	/**
	 * 用户退出系统，注销用户
	 * @return
	 * @throws Exception
	 */
	public String exitSystem() throws Exception {
		EmpAccount emp = (EmpAccount)this.getBaseform().getRequest().getSession().getAttribute("employee") ;
		this.getBaseform().getRequest().getSession().removeAttribute("employee") ;
		this.getBaseform().getRequest().getSession().removeAttribute("roleresMap") ;
		
		if(null != emp) {
			log.info("用户【"+emp.getEmployee().getEmpRealname()+" -- 账号:"+emp.getAccount()+"】正常退出系统...") ;
		} else {
			log.info("非法访问后台,IP:"+this.getBaseform().getRequest().getRemoteAddr()) ;
		}
		return "exitSystem" ;
	}

	public String getValResult() {
		return valResult;
	}

	@JSON(serialize = false, deserialize = false)
	public String getValUrl() {
		return valUrl;
	}

	public void setEmpMgr(EmployeeLoginLicenceManager empMgr) {
		this.empMgr = empMgr;
	}

	public void setValResult(String valResult) {
		this.valResult = valResult;
	}

	public void setValUrl(String valUrl) {
		this.valUrl = valUrl;
	}
	
}
